package com.ccl.iot.views;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ccl.iot.R;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.device.IOTSubBLir;

/**
 * 创建者     王浩
 * 创建时间   2017/5/23 15:19
 * 描述	      ${TODO}
 */

public class SubBlirView extends RelativeLayout {
    public static int STATE_NORMAL = 0;
    public static int STATE_DELETE = 1;

    private ImageView mIvBlir, mIvBlirDelete;
    private MarqueeLabel mMqlName;
    private IOTSubBLir mSubBLir;
    private CheckBox mCheckBox;
    private boolean mSelectable;

    public SubBlirView(Context context, boolean selectable) {
        super(context);
        mSelectable = selectable;
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_blirdev, this);
        mIvBlir = (ImageView) findViewById(R.id.iv_blirdev);
        mIvBlirDelete = (ImageView) findViewById(R.id.iv_delete_blirdev);
        mMqlName = (MarqueeLabel) findViewById(R.id.mql_blirdev_name);
        mCheckBox = (CheckBox) findViewById(R.id.cb_batch_selet);
        if(mSelectable){
            mCheckBox.setVisibility(View.VISIBLE);
        }
    }

    public void setSubDev(IOTSubBLir dev) {
        if (dev != null) {
            mIvBlir.setTag(dev);
            mIvBlir.setImageResource(DeviceIconProvider.getBlirSelectorId(dev.getDevType()));
            mSubBLir = dev;
            mMqlName.setText(dev.getName());
        }
    }

    public ImageView getIvBlirView() {
        return mIvBlir;
    }

    public IOTSubBLir getSubBLir() {
        return mSubBLir;
    }

    public void setOnBlirClickListener(OnClickListener l, OnLongClickListener ll) {
        if (l != null) {
            mIvBlir.setOnClickListener(l);
            mIvBlirDelete.setOnClickListener(l);
        }

        if (ll != null) {
            mIvBlir.setOnLongClickListener(ll);
        }
    }

    public void updateView(int state) {
        if (state == STATE_NORMAL) {
            mIvBlirDelete.setVisibility(View.GONE);
            mIvBlirDelete.setTag(null);
        } else if (state == STATE_DELETE) {
            mIvBlirDelete.setVisibility(View.VISIBLE);
            mIvBlirDelete.setTag(mSubBLir);
        }
    }
}
